/**
 * 
 */
package com.effectiv.gooruda.rest.service.impl;

import java.util.Date;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import lombok.extern.slf4j.Slf4j;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.rest.domain.Log;
import com.effectiv.gooruda.rest.ext.ExtResponse;
import com.effectiv.gooruda.rest.service.api.LogService;

/**
 * @author Dhrubo
 * 
 */
@Component
@Slf4j
public class LogServiceImpl extends LogService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#findBetween(java.util
	 * .Date, java.util.Date, int, int)
	 */
	public Response findBetween(Date start, Date end, int pageNo, int pageSize) {
		log.debug("start = {} , end = {}", start, end);
		log.debug("pageNo = {}, pageSize = {} ", pageNo, pageSize);
		
		return find(Criteria.where("createdDate").gte(start).lt(end), pageNo, pageSize);
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#findByText(java.lang
	 * .String, int, int)
	 */

	public Response findByText(String text, int pageNo, int pageSize) {
		log.debug("text = {} ", text);
		log.debug("pageNo = {}, pageSize = {} ", pageNo, pageSize);
		
		return find(Criteria.where("text").regex(text), pageNo, pageSize);
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#save(com.effectiv.gooruda
	 * .rest.domain.Log)
	 */
	public Response save(Log l) {
		log.info("Log = {}", l);
		
		validateLogType(l.getType());
		getTemplate().save(l, getCollection());
		
		ExtResponse res = new ExtResponse();
		res.setId(l.getId());
		res.setMessage("Log saved successfully.");
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#findByType(java.lang
	 * .String, int, int)
	 */
	public Response findByType(String type,
			int pageNo,
			int pageSize) {
		log.debug("type = {} ", type);
		log.debug("pageNo = {}, pageSize = {} ", pageNo, pageSize);
		
		validateLogType(type);
		return find(Criteria.where("type").is(type), pageNo, pageSize);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#countByType(java.lang
	 * .String)
	 */
	public Response countByType(String type) {
		validateLogType(type);
		
		return countDocuments(Criteria.where("type").is(type));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#countByText(java.lang
	 * .String)
	 */
	public Response countByText(String text) {
				
		return countDocuments(Criteria.where("text").regex(text));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.effectiv.gooruda.rest.service.api.LogService#countByDateRange(java
	 * .util.Date, java.util.Date)
	 */
	public Response countByDateRange(Date start, Date end) {
		log.debug("start = {} , end = {}", start, end);
		
		return countDocuments(Criteria.where("createdDate").gte(start).lt(end));
	}

}
